<template>
  <div>
    <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div :id="this.elId" :style="style"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "echartsIndex",

  props: {
    elId: {
      type: String,
      default: "",
    },
    echartData: {
      type: Object,
      default: {},
    },
    height: {
      type: Number,
      default: 100,
    },
    width: {
      type: Number,
      default: 100,
    },
  },

  data() {
    return {};
  },

  computed: {
    style() {
      return {
        width: this.width + "%",
        height: this.height + "px",
      };
    },
  },

  mounted() {
    this.getNewEchart();
  },

  methods: {
    // 创建 echarts 并渲染
    getNewEchart() {
      var chartDom = document.getElementById(this.elId);
      var myChart = echarts.init(chartDom);
      var option = this.echartData;
      myChart.setOption(option);
      window.addEventListener("resize", () => {
        if (myChart) {
          myChart.resize();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>